#!/usr/bin/python
# -*- coding:utf-8 -*-
import sys
import os
import config
#定义路径变量
picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'pic')
libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
if os.path.exists(libdir):    
    sys.path.append(libdir) 
    
from waveshare_epd import epd2in9_V2
import time
from PIL import Image,ImageDraw,ImageFont
import traceback
from borax.calendars.lunardate import LunarDate 

#打开天气数据文件，并读取内容赋值给a,b,c三个变量，以供函数调用。
# envConfig=Config()
weather_path=config.CONFIG['weather']
f=open(weather_path,'r')
a=f.readline()
a=a.rstrip()
b=f.readline()
b=b.rstrip()
c=f.readline()
c=c.rstrip()
f.close() 
def nianyueri():    
    #年月日星期位置    
    time_draw.rectangle((5, 5, 185, 25), fill = 255)    
    time_draw.text((5, 5), time.strftime('%Y年%m月%d日 %a'), font = font18, fill = 0)    
    newimage = time_image.crop([5, 5, 185, 25])    
    time_image.paste(newimage, (5,5)) 
def nongli():        
    #农历位置    
    today=LunarDate.today()    
    time_draw.rectangle((195, 5, 295, 25), fill = 255)    
    time_draw.text((195, 5), today.strftime('农 %M月%D'), font = font18, fill = 0)    
    newimage = time_image.crop([195, 5, 295, 25])    
    time_image.paste(newimage, (195,5)) 
def tianqi():    
    #天气位置    
    time_draw.rectangle((180,35, 295, 52), fill = 255)    
    time_draw.text((180, 35), a, font = font14, fill = 0)    
    newimage = time_image.crop([180, 35, 295, 52])    
    time_image.paste(newimage, (180,35)) 
def wendu():    
    #温度位置    
    time_draw.rectangle((180,55, 295, 72), fill = 255)    
    time_draw.text((180, 55), b, font = font14, fill = 0)    
    newimage = time_image.crop([180, 55, 295, 72])    
    time_image.paste(newimage, (180,55)) 
def fengxiang():        
    #风向位置    
    time_draw.rectangle((180,75, 295, 92), fill = 255)    
    time_draw.text((180, 75), c, font = font14, fill = 0)    
    newimage = time_image.crop([180, 75, 295, 92])    
    time_image.paste(newimage, (180,75)) 
def shidu():    
    #湿度位置    
    time_draw.rectangle((5,60, 35, 80), fill = 255,outline='black')    
    time_draw.text((5, 60), time.strftime('%S'), font = font18, fill = 0)    
    newimage = time_image.crop([5, 60, 35, 80])    
    time_image.paste(newimage, (5,602)) 
def richeng():        
    time_draw.rectangle((15,99, 170, 120), fill = 255)        
    time_draw.text((15, 99), time.strftime('努力、奋斗！'), font = font18, fill = 0)        
    newimage = time_image.crop([15, 99, 170, 120])        
    time_image.paste(newimage, (15,99)) 
def shijian():        
    #时间位置    
    time_draw.rectangle((35,30, 170, 80), fill = 255)    
    time_draw.text((35, 30), time.strftime('%H:%M'), font = font54, fill = 0)    
    newimage = time_image.crop([35, 30, 170, 85])    
    time_image.paste(newimage, (35,30))
    #测试的时候可以放try ,except，真正完成没有必要。
try:    
    #屏幕初始化    
    epd = epd2in9_V2.EPD()    
    epd.init()    
    epd.Clear(0xFF)    
    #定义4个尺寸字体。    
    font54 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 54)    
    font24 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)    
    font18 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 18)    
    font14 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 14)        
    # partial update局刷初始化    
    # epd=epd2in9_V2.EPD()    
    epd.init()    
    epd.Clear(0xFF)    
    #定义两个变量，以供调用。    
    time_image = Image.new('1', (epd.height, epd.width), 255)    
    time_draw = ImageDraw.Draw(time_image)    
    #执行不变化的项目，如：日期，农历，天气等。    
    nianyueri()    
    nongli()    
    tianqi()    
    wendu()    
    fengxiang()    
    shidu()    
    richeng()    
    epd.display_Base(epd.getbuffer(time_image))    
    #用循环来显示时间，因为时间必须要限时变0000化0的。    
    while (True):        
        shijian()        
        epd.display_Partial(epd.getbuffer(time_image))        
        #如果时间在11点30和0点1秒进行全局刷新。更新所有数据。 time.strftime('%H%M%S')=='000001' or         
        if time.strftime('%H%M%S')=='000001' or time.strftime('%H%M%S')=='033000':            
            epd.init()            
            epd.Clear(0xFF)            
            f=open(weather_path,'r')            
            a=f.readline()            
            a=a.rstrip()            
            b=f.readline()            
            b=b.rstrip()            
            c=f.readline()            
            c=c.rstrip()            
            f.close()            
            nianyueri()            
            nongli()            
            tianqi()            
            wendu()            
            fengxiang()            
            shidu()            
            richeng()            
            epd.display_Base(epd.getbuffer(time_image))        
        time.sleep(2)
except KeyboardInterrupt:    
    epd2in9_V2.epdconfig.module_exit()    
    exit()